﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using IB.Common.Base;
using IB.Paging;
using IB.Common.Entities;
using IB.Common;

namespace UIListProducts
{
    public partial class ListProducts : V_UIProductBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ProductID == -1)
                {
                    var Data = IB.Common.Base.V_Base.Data;

                    var news = from nw in Data.CV_Products
                               join cat_ in Data.CV_CatProducts on nw.CatID
                               equals cat_.CatID
                               where (nw.CatID == CatID || CatID == -1) && cat_.PortalID == PortalId
                               orderby nw.ProductID descending
                               select
                                   new ProductInfo
                                       {
                                           Content = nw.Content,
                                           CreateDate = nw.CreatedDate.Value,
                                           Id = nw.ProductID,
                                           ImageName = nw.ImageName,
                                           Price = nw.Price.Value,
                                           ShortContent = nw.ShortContent,
                                           Title = nw.Title
                                       };

                    //1 . chuyen truy van thanh list
                    IB.Paging.PagedList<ProductInfo> pl = new IB.Paging.PagedList<ProductInfo>(news, PageCurr - 1, PageSize);
                    //2 . gan vao repeater
                    if (pl.Count > 0)
                    {
                        rptproduct.DataSource = pl;
                        rptproduct.DataBind();
                        //3 . chuyen thanh thanh phan trang
                        IB.Paging.Pager pg = new IB.Paging.Pager(PageSize, PageCurr, pl.TotalItemCount, TabId);
                        //4. tao thanh phan trang
                        pnPaging.Text = pg.RenderHtml();
                    }
                    mv.SetActiveView(vList);
                }
                else
                {

                    //thiet lap giao dien chi tiet

                    string tempdetail = TempContentDetail;
                    string temprelative = TempContentRelative;

                    var Data = IB.Common.Base.V_Base.Data;
                    var news = (from nw in Data.CV_Products
                                where nw.ProductID == ProductID
                                select new ProductInfo
                                {
                                    Content = nw.Content,
                                    CreateDate = nw.CreatedDate.Value,
                                    Id = nw.ProductID,
                                    ImageName = nw.ImageName,
                                    Price = nw.Price.Value,
                                    ShortContent = nw.ShortContent,
                                    Title = nw.Title
                                }).FirstOrDefault();
                    if (news != null)
                    {
                        //this.txtTitle.InnerHtml = news.Title;
                        //this.txtDescript.InnerHtml = news.ShortContent;
                        //this.txtContent.InnerHtml = news.Content;

                        NewsTokenReplace newsTokenReplace = new NewsTokenReplace(news);

                        ltdetail.Text = newsTokenReplace.ReplaceNewsInfo(tempdetail);

                        var newsnewer = (from n in Data.CV_Products
                                         join cat_ in Data.CV_CatProducts on n.CatID
                                             equals cat_.CatID
                                         where n.ProductID > ProductID && cat_.PortalID == PortalId
                                         orderby n.ProductID ascending
                                         select new ProductInfo
                                         {
                                             Content = n.Content,
                                             CreateDate = n.CreatedDate.Value,
                                             Id = n.ProductID,
                                             ImageName = n.ImageName,
                                             Price = n.Price.Value,
                                             ShortContent = n.ShortContent,
                                             Title = n.Title
                                         }).Take(10);

                        var newolder = (from n in Data.CV_Products
                                        join cat_ in Data.CV_CatProducts on n.CatID
                                            equals cat_.CatID
                                        where n.ProductID < ProductID && cat_.PortalID == PortalId
                                        orderby n.ProductID ascending
                                        select new ProductInfo
                                        {
                                            Content = n.Content,
                                            CreateDate = n.CreatedDate.Value,
                                            Id = n.ProductID,
                                            ImageName = n.ImageName,
                                            Price = n.Price.Value,
                                            ShortContent = n.ShortContent,
                                            Title = n.Title
                                        }).Take(10);

                        rpt_Newer.DataSource = newsnewer;
                        rpt_Newer.DataBind();

                        rpt_Older.DataSource = newolder;
                        rpt_Older.DataBind();


                        mv.SetActiveView(vDetail);

                    }
                }
            }
        }

        protected void OnDataBinding(object sender, RepeaterItemEventArgs e)
        {
            ProductInfo item = (ProductInfo)e.Item.DataItem;
            ProductTokenReplace newsTokenReplace = new ProductTokenReplace(item);
            string str = newsTokenReplace.ReplaceProductInfo(TempContent);
            Literal lt = new Literal();
            lt.Text = str;
            e.Item.Controls.Add(lt);

        }

        protected void rpt_Newer_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ProductInfo item = (ProductInfo)e.Item.DataItem;
            ProductTokenReplace newsTokenReplace = new ProductTokenReplace(item);
            string str = newsTokenReplace.ReplaceProductInfo(TempContent);
            Literal lt = new Literal();
            lt.Text = str;
            e.Item.Controls.Add(lt);
        }

        protected void rpt_Older_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            ProductInfo item = (ProductInfo)e.Item.DataItem;
            ProductTokenReplace newsTokenReplace = new ProductTokenReplace(item);
            string str = newsTokenReplace.ReplaceProductInfo(TempContent);
            Literal lt = new Literal();
            lt.Text = str;
            e.Item.Controls.Add(lt);
        }

    }
}